﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.PublicShow
{
    public partial class SelectCustomer : BaseUI.BasePage
    {
        private Yx_CustomerBLL customerBLL = new Yx_CustomerBLL();
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();

        private string Model
        {
            get
            {
                return FORM_MODE.Value;
            }
            set
            {
                FORM_MODE.Value = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Model = Request["MODE"] == "" ? "MU" : Request["MODE"];

                FORM_COMPANY_ID.Value = -1;
            }
        }

        private void BindData()
        {
            var where = (string)ViewState["WHERE"];

            var _where = "N_COMPANY_ID IN (" + companyBLL.GetCompanyFatherAndChildByID(CurrentUserCompanyID) + ")";
            if (!string.IsNullOrEmpty(where))
            {
                _where += where;
            }
            var data = customerBLL.Where(_where);
            var companyData = companyBLL.GetCompanyFatherAndChild(CurrentUserCompanyID);
            foreach (DataRow item in companyData.Rows)
            {
                var cmyname = Convert.ToString(item["CMY_NAME"]);
                var cmyid = Convert.ToDecimal(item["CMY_ID"]);
                TreeNode t = new TreeNode(cmyname, cmyid.ToString());
                MyTree.Nodes.Add(t);
                BindCustomer(data, cmyid, t);
                MyTree.ExpandDepth = 1;
            }
        }

        private void BindCustomer(List<Yx_Customer> dt, decimal cmyid, TreeNode nodel)
        {
            var data = dt.Where(a => a.N_COMPANY_ID == cmyid).ToList();
            foreach (var item in data)
            {
                var itemnode = new TreeNode(item.C_NAME, item.N_ID.ToString());
                nodel.ChildNodes.Add(itemnode);
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            MyTree.Nodes.Clear();
            string whereStr = "";

            if (FORM_NAME.Text.Trim() != "")
            {
                if (FORM_PHONE.Text.Trim() != "")
                {
                    whereStr += " AND (C_NAME LIKE '%" + FORM_NAME.Text.Trim() + "%' AND C_PHONE LIKE '%" + FORM_PHONE.Text.Trim() + "%')";
                }
                else
                {
                    whereStr += " AND C_NAME LIKE '%" + FORM_NAME.Text.Trim() + "%'";
                }
            }
            else
            {
                if (FORM_PHONE.Text.Trim() != "")
                {
                    whereStr += " AND C_PHONE LIKE '%" + FORM_PHONE.Text.Trim() + "%'";
                }
            }
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " AND N_COMPANY_ID = " + FORM_COMPANY_ID.Value;
            }
            ViewState["WHERE"] = whereStr;
            BindData();
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            TreeNodeCollection tnc = MyTree.CheckedNodes;
            try
            {
                var nodes = MyTree.Nodes;
                int count = 0;
                string Ids = "";
                string Name = "";
                foreach (TreeNode item in nodes)
                {
                    if (item.Checked)
                    {
                        foreach (TreeNode itm in item.ChildNodes)
                        {
                            Ids += itm.Value + ",";
                            Name += itm.Text + ",";
                            count++;
                        }
                    }
                    else
                    {
                        foreach (TreeNode itm in item.ChildNodes)
                        {
                            if (itm.Checked)
                            {
                                Ids += itm.Value + ",";
                                Name += itm.Text + ",";
                                count++;
                            }
                        }
                    }
                }
                if (count > 0)
                {
                    Ids = Ids.Substring(0, Ids.Length - 1);
                    Name = Name.Substring(0, Name.Length - 1);
                }
                if (Model == "SINGELE" && count != 1)
                {
                    MessageBox.Show(Page, "请选择一个客户公司");
                    return;
                }
                /*
                if (SelectType == "单选" && count != 1)
                {
                    MessageBox.Show(Page, "请选择一个用户");
                    return;
                }
                if (count <= 0)
                {
                    MessageBox.Show(Page, "请至少选择一个用户");
                    return;
                }
                */

                //Name = Ids.Substring(0, +Ids.LastIndexOf(',')) + "%" + Name.Substring(0, Name.LastIndexOf(','));
                MessageBox.ResponseScript(this, "FORM_SUBMIT(\"" + Ids + "%" + Name + "\");");
                return;
            }
            catch (Exception exp)
            {
                MessageBox.Show(Page, "【异常：】" + exp.Message.ToString());
                return;
            }
        }
    }
}